import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';

import '../../../services/address.dart';
import '../../../services/dio_manager.dart';
import '../../../utils/persisten_storage.dart';
import '../../bottom_tab_controller.dart';
import '../../home_module/home_view.dart';
import '../login_model.dart';

class CodeLoginController extends GetxController with GetSingleTickerProviderStateMixin{


  var username = TextEditingController();

  var pwd = TextEditingController();

  var options = '+86';

  /// 注册请求
  requestDataWithCodeLogin(var login,var password)async{

    var params = {
      'login':login,
      'code':password,
      'type':'1'
    };

    var json = await DioManager().kkRequest(Address.login,bodyParams: params,isShowLoad: true);

    Usesr model = Usesr.fromJson(json);

    if (model.errmsg == 'ok') {
      // await prefs.setString('acctoken', _model.data.accessToken);
      PersistentStorage().setStorage('acctoken', model.data!.accessToken);
      PersistentStorage().setStorage('uid', model.data!.uid);
      PersistentStorage().setStorage('partner_id', model.data!.partnerId);
      PersistentStorage().setStorage('image128', model.data!.image128);
      PersistentStorage().setStorage('job_company_id', model.data!.jobCompanyId.toString());
      PersistentStorage().setStorage('job_user_id', model.data!.jobUserId.toString());
      PersistentStorage().setStorage('name', model.data!.name);
      PersistentStorage().setStorage('login_mobile', model.data!.login_mobile);
      PersistentStorage().setStorage('companyIdName', model.data!.companyIdName);
      PersistentStorage().setStorage('companyId', model.data!.companyId.toString());

      Get.offAll(Tabs());


    } else {
      BotToast.showText(text: json['message']);
    }

  }
  /// 点击注册按钮
  tapClickRegister(){
    if (username.text.isEmpty) {
      // Toast.toast(context,msg: '请输入手机号码');
      BotToast.showText(text: '请输入手机号码');
      return;
    }
    if (pwd.text.isEmpty) {
      // Toast.toast(context,msg: '请输入手机号码');
      BotToast.showText(text: '请输入验证码');
      return;
    }
    if(!GetUtils.isPhoneNumber(username.text)){
      BotToast.showText(text: '请输入正确的手机号码');
      return;
    }
    requestDataWithCodeLogin(username.text.replaceAll(RegExp(r"\s+\b|\b\s"), ""), pwd.text);

  }
}